import path from "path";
import server from "./server.js";
import { fileURLToPath } from "url";
import watchUsb from "./watch-usb.js";
import { BrowserWindow, nativeImage } from "electron";
import { exposeIcpMainHandlerForWeb } from "./ipcmain-handler/inject.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const createMainWindow = () => {
  // 创建主应用窗口
  const mainWinOtp = {
    width: 900,
    height: 600,
    minWidth: 900,
    minHeight: 600,
    resizable: false,
    webPreferences: {
      webSecurity: false, // 禁用同源策略，允许跨域
      preload: path.join(__dirname, "./preload.js"),
    },
    icon: path.join(__dirname, "../assets/winico.png"),
    // title: '长安助手',
    titleBarStyle: "hidden",
    titleBarOverlay: {
      color: "rgba(0,0,0,0)",
      height: 35,
      symbolColor: "white",
    },
  };
  const mainWin = new BrowserWindow(mainWinOtp);
  watchUsb(mainWin);
  exposeIcpMainHandlerForWeb(mainWin);
  mainWin.setMaximizable(false);
  server(mainWin);
  return mainWin;
};

export const createAboutWindow = () => {
  const mainWin = global.app.mainWindow;
  // 创建关于应用窗口
  const aboutWinOtp = {
    parent: mainWin,
    // modal: true,
    width: 300,
    height: 200,
    resizable: false,
    webPreferences: {
      webSecurity: false,
      preload: path.join(__dirname, "./preload.js"),
    },
    backgroundColor: "#fff",
    icon: nativeImage.createEmpty(),
    autoHideMenuBar: true, // 子窗口不需要在要完整的父窗口的菜单栏
    // frame: false, // 隐藏标题栏
    title: "关于",
  };
  const aboutWin = new BrowserWindow(aboutWinOtp);
  exposeIcpMainHandlerForWeb(aboutWin);
  aboutWin.setMinimizable(false); // 隐藏窗口的最小化按钮
  aboutWin.setMaximizable(false); // 隐藏窗口的最大化按钮
  server(aboutWin, "/about");
  return aboutWin;
};
